
from django.shortcuts import render
from commodity.models import Types,CommodityInfos
from django.views.generic import TemplateView

def page_not_found(request, exception):
    return render(request, '404.html', status=404)


def page_error(request):
    return render(request, '404.html', status=500)
#
#
# # Create your views here.
#
# def page_not_found(request, exception):
#     # 渲染自定义 404 模板，状态码设为 404
#     return render(request, '404.html', status=404)
#
# def indexView(request):
#     title="首页"
#     classContent=""
#
#     commodityInfos=CommodityInfos.objects.order_by("-sold").all()[:8]
#     #获取所有商品分类
#     types=Types.objects.all()
#     #筛选儿童服饰分类下的二级分类，列表形式返回
#     c1 = [x.seconds for x in types if x.first == '儿童服饰']
#     #获取儿童服饰最高销量商品
#     clothes = CommodityInfos.objects.filter(types__in=c1).order_by('-sold')[:5]
#     #筛选奶粉辅食分类下的二级分类
#     f1 = [x.seconds for x in types if x.firsts =='奶粉辅食']
#     #获取奶粉辅食销量最高五个
#     food=CommodityInfos.objects.filter(types__in=f1).order_by('-sold')[:5]
#     #
#     g1 = [x.seconds for x in types if x.first == '宝宝用品']
#     # 获取儿童服饰最高销量商品
#     goods = CommodityInfos.objects.filter(types__in=g1).order_by('-sold')[:5]
#
#     return render(request, "index.html", locals())
#


class indexClassView(TemplateView):

    template_name = 'index.html'

    template_engine=None

    content_type = None

    extra_context = {'title':'首页','classContent':''}

    def get_context_data(self, **kwargs):
        context =super(indexClassView,self).get_context_data(**kwargs)
        context['commodityInfos']=CommodityInfos.objects.order_by('-sold')[:8]

        types=Types.objects.all()

        cl=[x.seconds for x in types if x.firsts=='儿童服饰']
        context['clothes']=CommodityInfos.objects.filter(types__in=cl).order_by('-sold')[:5]
        fl=[x.seconds for x in types if x.firsts=='奶粉辅食']
        context['food']=CommodityInfos.objects.filter(types__in=fl).order_by('-sold')[:5]
        gl=[x.seconds for x in types if x.firsts=='儿童用品']
        context['clothes']=CommodityInfos.objects.filter(types__in=gl).order_by('-sold')[:5]

        return context

    def get(self,request,*args,**kwargs):
        #处理HTTP GET请求
        context=self.get_context_data(**kwargs)

        return self.render_to_response(context)

    def post(self,request,*args,**kwargs):

        context=self.get_context_data(**kwargs)
        return self.render_to_response(context)




